package com.jjlin.leetcode;

class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newhead = new ListNode(-1);//辅助节点
        newhead.next = head;
        ListNode slow = newhead, fast = slow;//快慢指针
        for(int i = 0; i < n; i++)
            fast = fast.next;
        while(fast.next != null){//找出倒数第n个节点的前一个节点：slow
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;//删除第n个节点
        return newhead.next;
    }
}
